<?php
namespace app\jxcshop\controller\sys_admin;
use app\AdminController;
use think\Db;

use app\distribution\model\DividendModel;

//*------------------------------------------------------ */
//-- 佣金明细
/*------------------------------------------------------ */
class JiaquanfenhongLog extends AdminController{
	//*------------------------------------------------------ */
	//-- 初始化
	/*------------------------------------------------------ */
    public function initialize(){
   		parent::initialize();
        $this->Model = new \app\jxcshop\model\JiaquanfenhongLog();
    }

	//*------------------------------------------------------ */
	//-- 首页
	/*------------------------------------------------------ */
    public function index(){

		$this->getList(true);
        return $this->fetch('index');
    }
	/*------------------------------------------------------ */
    //-- 获取列表
	//-- $runData boolean 是否返回模板
    /*------------------------------------------------------ */
    public function getList($runData = false,$is_cancel = false){

		$search['stage'] = input('stage','','trim');
        $search['user_id'] = input('user_id','','trim');
		$where = [];
		if ($search['stage'] >= 0 && !empty($search['stage'])){
			$where[] = ['stage','=',$search['stage']];
		}
        if ($search['user_id'] >= 0 && !empty($search['user_id'])){
            $where[] = ['user_id','=',$search['user_id']];
        }

        $data = $this->getPageList($this->Model, $where);

        foreach ($data['list'] as $k=>$v){
            $data['list'][$k]['username'] = $this->getUserfieldname($v['user_id'],'nick_name');
            $data['list'][$k]['rolename'] = $this->getUserrole($v['user_id']);
            $data['list'][$k]['allamount'] = $this->getallamount($v['yuefen']);
        }
		$this->assign("data", $data);
		$this->assign("search", $search);
		if ($runData == false){
			$data['content'] = $this->fetch('list')->getContent();
			unset($data['list']);
			return $this->success('','',$data);
		}
        return true;
    }

    /*
     * 会员身
     */
    public function getUserfieldname($user_id,$field = 'nick_name'){
        $userObj = Db::table("users")->field($field)->where("user_id","=",$user_id)->find();

        return $userObj[$field];
    }

    /*
     * 获取会员身份
     */
    public function getUserrole($user_id){
        $userObj = Db::table("users")->where("user_id","=",$user_id)->find();
        if($userObj['role_id'] == 0){
            return "粉丝";
        }else if($userObj['role_id'] == 1){
            return "代理";
        }else if($userObj['role_id'] == 2){
            return "经理";
        }else if($userObj['role_id'] == 3){
            return "会员";
        }
    }

    public function getallamount($yuefen){
        $datemonth = intval($yuefen);
        $start_time = strtotime(date("Y-$datemonth-01 00:00:00"));
        $end_time  = strtotime(date("Y-$datemonth-t 23:59:59"));
        $where[] = ['order_status','=',1];
        $where[] = ['pay_status','in','1,2'];
        $where[] = ['add_time','>=' ,$start_time];
        $where[] = ['add_time','<=' ,$end_time];

        $orderRes = Db::table("shop_order_info")->where($where)->sum("order_amount"); //上个月平台总业绩
        if($orderRes > 0){
            return $orderRes;
        }else{
            return 0;
        }


    }

}
